<template>
  <!-- 登录框 -->
  <div class="loginColor">
    <div class="login-box">
      <!-- 头像 -->
      <div>
        <img src="../assets/logo.png" alt />
      </div>
      <!-- input框 -->
      <el-form class="loginFrom" :model="loginFrom" :rules="rules" ref="loginReset">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-yonghu" v-model="loginFrom.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-mima" v-model="loginFrom.password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <!-- 登陆时有一个预验证 -->
          <el-button class="loginBtn" @click="login">登录</el-button>
          <el-button class="resetBtn" @click="loginRef">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginFrom: { username: "admin", password: "123456" },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "长度在 6 到 16 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    loginRef() {
      console.log(this);
      this.$refs.loginReset.resetFields();
    },
    login() {
      this.$refs.loginReset.validate(async validate => {
        if (!validate) return;
        //   因为用axios发送请求都会返回 一个promise对象 所以要想拿到其中的值 可以用es7中的async和await
        const { data } = await this.$http.post("login", this.loginFrom);
        console.log(data);
        if (data.meta.status != 200) return this.$message.error(data.meta.msg);
        this.$message.success(data.meta.msg);
        // 所有需要展示的页面都去要携带token信息 否则没有办法加载页面
        console.log(data.data.token);
        // 将token数据存放在sessionStorage当中
        window.sessionStorage.setItem('token',data.data.token)
        this.$router.push('/home')
        
      });
    }
  }
};
</script>

<style lang="less" scoped>
.loginColor {
  background-color: #674e85;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 320px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10%;
  > div {
    border: 1px solid #f1f1f1;
    width: 130px;
    height: 130px;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    // box-shadow: h-shadow v-shadow blur spread color inset;
    box-shadow: 0 0 10px #c7b7d1;
  }
  .loginFrom {
    //   margin: 0 20px;
    //   display: flex;
    position: absolute;
    bottom: 0;
    width: 100%;
    // box-sizing真对padding
    // margin-left: 20px;
    padding: 0 20px;
    box-sizing: border-box;
  }
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #c7b7d1;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
  .loginBtn {
    background-color: #eba9cf;
  }
  .resetBtn {
    background-color: #c7b7d4;
  }
}
</style>